#include "cdownloader.h"

CDownloader::CDownloader()
{
    m_reply = 0;
    connect(&manager, SIGNAL(finished(QNetworkReply*)),
            SLOT(downloadFinished(QNetworkReply*)));

    m_error = 0;
    m_is404 = false;
}

CDownloader::~CDownloader()
{
    if(m_reply)
        delete m_reply;
}

void CDownloader::doDownload(const QUrl &url)
{
    QNetworkRequest request(url);
    QNetworkReply *reply = manager.get(request);
    connect(reply, SIGNAL(sslErrors(QList<QSslError>)), reply, SLOT(ignoreSslErrors()));
}

void CDownloader::downloadFinished(QNetworkReply *reply)
{
    if(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt()==404)
    {
        m_is404 = true;
        emit finished();
        return;
    }

    m_reply = reply;
    if (reply->error())
    {
        m_error = reply->error();
        m_errorStr = reply->errorString();
        emit finished();
        return;
    }

    m_data = reply->readAll();

    emit finished();
}
